﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Spider.Utility;

namespace Spider.Visited
{
   public class DBVisited:IVisited
    {
       public int GetCount()
       {
           var obj=SqlHelper.ExecuteScalar("select count(*) from Visited");
           return Convert.ToInt32(obj);
       }

       public void AddUrl(string url, VisitedData data)
       {
           SqlParameter[] para = new SqlParameter[]
           {
               new SqlParameter("@Url",url),
               new SqlParameter("@Date",DateTime.Now.ToString())
           };

           SqlHelper.ExecuteNonQuery("insert into Visited(Url,CreateOn) values(@Url,@Date)", para);
       }

       public bool ContainUrl(string url)
       {
           SqlParameter[] para = new SqlParameter[]
           {
               new SqlParameter("@Url",url),
           };
           var obj = SqlHelper.ExecuteScalar("select count(*) from Visited where Url=@Url",para);
           if(Convert.ToInt32(obj)>0)
               return true;
           return false;
       }

       public VisitedData GetUrl(string url)
       {
           SqlParameter[] para = new SqlParameter[]
           {
               new SqlParameter("@Url",url),
           };
           var obj = SqlHelper.ExecuteScalar("select * from Visited where Url=@Url",para);
           return obj as VisitedData;
       }

       public void RemoveUrl(string url)
       {
           SqlParameter[] para = new SqlParameter[]
           {
               new SqlParameter("@Url",url),
           };
           SqlHelper.ExecuteNonQuery("delete from Visited where Url=@Url", para);
       }
    }
}
